package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	user "user_server/proto"
)

type UserServiceClient interface {
	GetUserDetail(ctx context.Context, req *user.UserDetailRequest) (*user.UserDetailResponse, error)
}

type userServiceClient struct {
	client user.UserClient
}

func NewUserServiceClient(conn *grpc.ClientConn) UserServiceClient {
	return &userServiceClient{
		client: user.NewUserClient(conn),
	}
}

func (c *userServiceClient) GetUserDetail(ctx context.Context, req *user.UserDetailRequest) (*user.UserDetailResponse, error) {
	return c.client.GetUserDetail(ctx, req)
}

type UserHandler func(ctx context.Context, client user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler UserHandler) (interface{}, error) {
	dial, err := grpc.Dial(fmt.Sprintf("%s:%d", "127.0.0.1", 9991), grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := user.NewUserClient(dial)
	return handler(ctx, client)
}
func UserLogin(ctx context.Context, in *user.LoginRequest) (*user.LoginResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.Login(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LoginResponse), nil
}

func UserRegister(ctx context.Context, in *user.RegisterRequest) (*user.RegisterResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.Register(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.RegisterResponse), nil
}
func UserSignIn(ctx context.Context, in *user.SignInRequest) (*user.SignInResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.SignIn(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.SignInResponse), nil
}

func UserRetroactiveSign(ctx context.Context, in *user.RetroactiveSignRequest) (*user.RetroactiveSignResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.RetroactiveSign(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.RetroactiveSignResponse), nil
}

func UserLuckyDraw(ctx context.Context, in *user.LuckyDrawRequest) (*user.LuckyDrawResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.LuckyDraw(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LuckyDrawResponse), nil
}

func UserRealName(ctx context.Context, in *user.UserRealNameReq) (*user.UserRealNameResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.UserRealName(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UserRealNameResp), nil
}
func UserFindLucky(ctx context.Context, in *user.GetLuckyDrawRecordsRequest) (*user.GetLuckyDrawRecordsResponse, error) {
	userClient, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.GetLuckyDrawRecords(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return userClient.(*user.GetLuckyDrawRecordsResponse), nil
}

func UserFindUserInfo(ctx context.Context, in *user.GetSignInRecordsRequest) (*user.GetSignInRecordsResponse, error) {
	userClient, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.GetSignInRecords(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return userClient.(*user.GetSignInRecordsResponse), nil
}

func GetUserDetail(ctx context.Context, in *user.UserDetailRequest) (*user.UserDetailResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		res, err := client.GetUserDetail(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UserDetailResponse), nil
}
